#ifndef __THREAD_HPP__
#define __THREAD_HPP__
#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
#include <functional>
namespace ThreadModule
{
    template <typename T>
    using func_t = std::function<void(T &)>;
    template <typename T>
    class Thread
    {
    public:
        Thread(func_t<T> func, T& data, const std::string &threadname = "none_name") // 为什么--常量引用和非常量引用的概念
            : _func(func), _data(data), _threadname(threadname), _stop(true)
        {
        }
        void Excute()
        {
            _func(_data);
        }
        static void *handler(void *args)
        {
            Thread<T> *self = static_cast<Thread<T> *>(args);
            // self->Excute();
            self->_func(self->_data);
            return nullptr;
        }
        bool Start()
        {
            int ret = pthread_create(&_tid, nullptr, handler, this);
            if (ret == 0)
            {
                _stop = false;
                return true;
            }
            else
            {
                return false;
            }
        }
        void Join()
        {
            if (!_stop)
            {
                pthread_join(_tid, nullptr);
            }
        }
        std::string name()
        {
            return _threadname;
        }
        ~Thread() {}

    private:
        std::string _threadname;
        pthread_t _tid;
        T& _data;
        func_t<T> _func;
        bool _stop;
    };
}
#endif